package com.sgb.auction.viewmodule;

import android.app.Dialog;

import androidx.lifecycle.ViewModel;

import com.sgb.auction.activity.AuctionManageActivity;
import com.sgb.auction.base.BaseApi;
import com.sgb.auction.entity.AuctionManageListEntity;
import com.sgb.auction.entity.RequestBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/11/19
 * AUTHOR:F_Q
 * Description:
 */
public class AuctionManageModel  extends ViewModel {
    AuctionManageActivity activity;
    private boolean isLastPage;

    public void setActivity(AuctionManageActivity auctionManageActivity) {
        this.activity = auctionManageActivity;
    }

    /**
     * 拍卖管理列表
     * @param requestBean
     */
    public void AuctionManageList(RequestBean requestBean) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAuctionManageList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<AuctionManageListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<AuctionManageListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    activity.setAuctionManageList(data.getData().getList(),isLastPage);
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    public void getWebView(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("auctionDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?no="+no;
                    BaseApi.startWeb(activity, webUrl);
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    public void getAuvtionState() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAuctionState().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    activity.setAuvtionStateList(data.getData());
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }

    /**
     * 调用撤拍接口
     * @param no
     * @param content
     * @param dialog
     */
    public void getAuctionCancel(String no, String content, Dialog dialog) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("no",no);
        map.put("reason",content);
        BaseApi.getApi().getAuctionCancel(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    activity.setAuctionCancel(dialog,data.getMsg());
                }else {
                    MToast.showToast(activity,data.getMsg());
                }
            }
        });
    }
}
